import router from '@/router'
import mitt from 'mitt'

export const addUserRouter = (menuList: any[]) => {
  menuList?.forEach((menu) => {
    if (menu?.children?.length > 0) {
      addUserRouter(menu?.children)
    } else if (menu?.component && menu?.component !== 'layout/layout') {
      const component = import.meta.glob('@/views/**/*.vue')[`/src/views/${menu?.component}.vue`]
      if (component) {
        router.addRoute('home', {
          path: menu?.menuUrl,
          name: menu?.menuCode,
          component: component,
          meta: {
            title: menu?.menuName,
            icon: menu?.menuIcon
          }
        })
      }
    }
  })
}

export const bus = mitt()
